using System;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web;

/// <summary>
/// Chris Blankenship
/// v 1.0
/// www.chrisbcode.com / www.dscoduc.com 
///
/// Adds a Google Analytics Tracking Code to your website.
/// </summary>
[Extension("Adds Google Analytics code to BlogEngine", "1.0", "<a target=\"_blank\" href=\"http://www.chrisbcode.com\">Chris Blankenship</a>")]
public class GoogleAnalytics
{
    static protected ExtensionSettings _settings = null;
    static protected string Key
    {
        get
        {
            return _settings.GetSingleValue("trackerID");
        }
    }

    public GoogleAnalytics()
	{
        BlogEngine.Core.Post.Serving += new EventHandler<ServingEventArgs>(PrepareGoogleAnalytics);
        BlogEngine.Core.Page.Serving += new EventHandler<ServingEventArgs>(PrepareGoogleAnalytics);

        ExtensionSettings settings = new ExtensionSettings("GoogleAnalytics");
        settings.AddParameter("trackerID", "Google Tracking Code", 50, true);

        // Load generic default value to illustrate the format
        settings.AddValue("trackerID", "UA-XXXXXXX-Y");

        settings.Help = "<br />Add Google Analytics to your website<br /><br />www.chrisbcode.com";

        settings.IsScalar = true;
        ExtensionManager.ImportSettings(settings);
        _settings = ExtensionManager.GetSettings("GoogleAnalytics");
    }

    private void PrepareGoogleAnalytics(object sender, ServingEventArgs e)
    {
        HttpContext context = HttpContext.Current;
        if (context.CurrentHandler is System.Web.UI.Page)
            // Make sure we haven't already added the script to the page header.
            if (context.Items["GoogleTracker"] == null)
            {
                // prep page handler
                System.Web.UI.Page page = (System.Web.UI.Page)context.CurrentHandler;
                
                // Add Google Tracker
                StringBuilder trackerScript = new StringBuilder();
                string connectionString = "";

                trackerScript.AppendLine("");
                trackerScript.AppendLine("<!-- Begin Google scripts -->");
                if (HttpContext.Current.Request.IsSecureConnection) { connectionString = "https://ssl."; } else { connectionString = "http://www."; }
                trackerScript.AppendLine("<script type=\"text/javascript\">");
                trackerScript.AppendLine("<script src='" + connectionString + "google-analytics.com/ga.js' type='text/javascript'></script>");
                trackerScript.AppendLine("</script>");

                trackerScript.AppendLine("<script type=\"text/javascript\">");
                trackerScript.AppendLine("var pageTracker = _gat._getTracker('" + Key + "');");
                trackerScript.AppendLine("pageTracker._initData();");
                trackerScript.AppendLine("pageTracker._trackPageview();");
                trackerScript.AppendLine("</script>");
                trackerScript.AppendLine("<!-- End Google scripts -->");
                trackerScript.AppendLine("");

                // finish up                
                page.Header.Controls.Add(new System.Web.UI.LiteralControl(trackerScript.ToString()));
                context.Items["GoogleTracker"] = 1;
            }
    }
}
